#include "widget.h"
#include "ui_widget.h"
#include <QProcess>
#include <QStandardPaths>
#include <QDateTime>
#include <QDir>
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{

    ui->setupUi(this);
    initApp();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::installAppFilter()
{
    ui->lineEditSourcePath->installEventFilter(this);
    ui->lineEditTargetPath->installEventFilter(this);
}
void Widget::bindConnect()
{
    connect(this,&Widget::dblMouseButtonSignal,[=](QString arg1){
        QString dir;
        if(arg1.toUpper() == "SOURCE")
        {
            dir =  QFileDialog::getOpenFileName(NULL,"Open Folder","c:/");
            ui->lineEditSourcePath->setText(dir);
        }
        else if(arg1.toUpper() == "TARGET")
        {
            dir =  QFileDialog::getExistingDirectory(NULL,"Open Folder","c:/");
            ui->lineEditTargetPath->setText(dir);
        }
    });
}
void Widget::initApp()
{
    QString desktop_path = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
    QString today = QDateTime::currentDateTime().toString("yyyyMMdd");
    m_tempDir = QString("%1/%2%3").arg(desktop_path).arg("Temp").arg(today);
    m_msgID.clear();
    m_AppID.clear();
    m_MAC.clear();
    ui->lineEditMsgID->setPlaceholderText(QString(tr("99NISPLC0008")));
    ui->lineEditAppID->setPlaceholderText("E59901");
    ui->lineEditSourcePath->setPlaceholderText("D:/Test/Msg_B05_finance.xml");
    ui->lineEditTargetPath->setPlaceholderText("D:/Test/Msg_AppID.xml");
    ui->textEditLog->setPlaceholderText("日志打印\r\n\r\n源路径和目标路径可以通过鼠标左键双击打开");
    ui->lineEditName->setPlaceholderText("理财签约");
    ui->lineEditMac->setPlaceholderText("true|false");
    const QPixmap pixmap("://logo.ico");
    QIcon icon(pixmap);
    this->setWindowIcon(icon);
    this->setWindowTitle("View Manager");
    bindConnect();
    installAppFilter();
    checkTargetSaveFile();
}
void Widget::checkTargetSaveFile()
{
    QDir dir(m_tempDir);
    if(!dir.exists())
    {
        if(!dir.mkpath(m_tempDir))
        {
            QMessageBox::critical(this,"Err","Create Directory Err ");
            return;
        }
    }
}
void Widget::ReadMsg(QString &filePath)
{
   m_XMLList.clear();
   QFile file(filePath);
   if(!file.exists())
   {
       qDebug() << " file not exist";
       return;
   }
   file.encodeName("UTF-8");
   if(!file.open(QIODevice::ReadOnly))
   {
       qDebug() << "Open file fail";
   }
   //文件关联doc
   QDomDocument doc;
   if(!doc.setContent(&file))
   {
       qDebug() << "doc setContent fail";
       return;
   }
   qDebug() << "success!";
   file.close();
   //获取根元素
   QDomElement root =doc.documentElement();
   //判断根元素是否有子元素
   if(!root.hasChildNodes())
   {
       qDebug() << "have child";
       return;
   }
   QDomElement firstChild = ParseNode(root,1);
   ParseNode(firstChild,2);
   qDebug() <<"m_XMLList =" << m_XMLList.size();
   for(int i=0; i<m_XMLList.size();i++)
   {
        qDebug() << " -----------------";
        qDebug() << m_XMLList.at(i).m_strId;
        qDebug() <<m_XMLList.at(i).m_tagName;
        qDebug() <<m_XMLList.at(i).m_strValue;
        qDebug() <<m_XMLList.at(i).m_strResponsePath;
        qDebug() << " -----------------";
   }
}
QDomElement& Widget::ParseNode(const QDomElement &root, int arg)
{
    //获取所有的子节点
    QDomElement *targetEmt = new QDomElement;
    QDomNodeList list = root.childNodes();
    for(int i=0; i < list.size();i++)
    {
        targetEmt = &list.at(i).toElement();

        if(m_msgID == targetEmt->attribute("id") && arg == 1)
        {
            qDebug() << "find emt";
            break;

        }
        else if(arg ==2)
        {
            QString targetName = targetEmt->tagName();
            QDomNodeList slist = targetEmt->childNodes();
            for(int k=0; k <slist.count(); k++)
            {
                QDomElement sTargetEmt = slist.at(k).toElement();

                if(sTargetEmt.tagName() == "F_TranCode" && targetName.toUpper() =="REQUEST")
                {
                    m_XMLRoot.m_strFlag ="Request";
                    m_XMLRoot.m_strId = sTargetEmt.tagName();
                    m_XMLRoot.m_strRequestPath = sTargetEmt.attribute("Request");
                    m_XMLRoot.m_strValue = sTargetEmt.attribute("value");
                    m_XMLRoot.m_strResponsePath =sTargetEmt.attribute("Response");
                    m_XMLRoot.m_strRemark = sTargetEmt.attribute("remark");
                    qDebug() << "--------------------" << sTargetEmt.attribute("Request");
                    m_XMLList.push_back(m_XMLRoot);
                }
                else
                {
                    //Request
                    if(targetName.toUpper() =="REQUEST")
                    {
                        QString reqPath = sTargetEmt.attribute("Request");
                        QStringList reqList = reqPath.split("/");
                        QString resLabel = reqList.at(reqList.count()-1);
                        m_XMLRoot.m_strFlag ="Request";
                        m_XMLRoot.m_strId = QString("F_%1_%2").arg(m_AppID).arg(resLabel);
                        m_XMLRoot.m_strRequestPath = reqPath;
                        m_XMLRoot.m_strValue = QString("$%1_P_%2").arg(m_AppID).arg(resLabel);
                        m_XMLRoot.m_strResponsePath ="";
                    }
                    else
                    {
                        //Response
                        QString respPath = sTargetEmt.attribute("Response");
                        QStringList respList =respPath.split("/");
                        QString respLabel =respList.at(respList.count()-1);
                        m_XMLRoot.m_strFlag ="Response";
                        m_XMLRoot.m_strId = QString("F_%1_%2").arg(m_AppID).arg(respLabel);
                        m_XMLRoot.m_strRequestPath = QString("");
                        m_XMLRoot.m_strValue = QString("$%1_RP_%2").arg(m_AppID).arg(respLabel);
                        m_XMLRoot.m_strResponsePath =respPath;

                    }
                    m_XMLRoot.m_strRemark = sTargetEmt.attribute("remark");
                    m_XMLList.push_back(m_XMLRoot);

                }

            }


        }
    }
    return *targetEmt;
}
void Widget::createXML(QString &filePath)
{
    QFile file(filePath);
    if(file.exists())
    {
        return;
    }
    file.encodeName("UTF-8");
    if(!file.open(QIODevice::WriteOnly))
    {
        return;
    }
    QDomDocument doc;
    QDomProcessingInstruction ins;
    ins =doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"utf-8\"");
    doc.appendChild(ins);
    QDomElement root =doc.createElement("Body");
    QDomAttr rAttr = doc.createAttribute("useHEX");
    rAttr.setNodeValue("false");
    root.setAttributeNode(rAttr);
    doc.appendChild(root);
    QTextStream stream(&file);
    doc.save(stream,4);
    file.close();
}
void Widget::appendXML(QString &filePath, XMLFileList &fileList, QString &msgID, QString &msgName,QString &Mac)
{
    QFile file(filePath);
    file.encodeName("UTF-8");
    //先读 ，后写
    if(!file.open(QIODevice::ReadOnly))
    {
        qDebug() << " open fail";
        return;
    }
    QDomDocument doc;
    if(!doc.setContent(&file))
    {
        qDebug() << " setContent fail";
        return;
    }
    file.close();
    //获取根节点
    QDomElement root = doc.documentElement();
    if(root.hasChildNodes())
    {
        //追加先判断Msg 是否存在
        QDomNodeList fNodeList = root.childNodes();
        qDebug() << "fNodeList " << fNodeList.count();
        for(int i=0; i < fNodeList.count(); i++)
        {
           QDomElement fEmt = fNodeList.at(i).toElement();
           if(fEmt.attribute("id") == msgID)
           {
               QMessageBox::critical(NULL,"ERR","The MsgID key is repeated");
               return;
           }

        }
    }
    //不存在子节点
    QDomElement msgEmt = doc.createElement("Msg");
    //插入顺序随机
    QDomAttr msgAttr = doc.createAttribute("id");
    msgAttr.setNodeValue(msgID);
    msgEmt.setAttributeNode(msgAttr);
    msgAttr =doc.createAttribute("mac");
    msgAttr.setNodeValue(Mac);
    msgEmt.setAttributeNode(msgAttr);
    msgAttr =doc.createAttribute("name");
    msgAttr.setNodeValue(msgName);
    msgEmt.setAttributeNode(msgAttr);
    root.appendChild(msgEmt);
    ui->textEditLog->append(QString("id = %1").arg(msgID));
    ui->textEditLog->append(QString("mac = %1").arg(Mac));
    ui->textEditLog->append(QString("<Msg id =\"%1\" name = \"%2\" mac=\"%3\">")
                            .arg(msgID)
                            .arg(msgName)
                            .arg(Mac));
    //一级节点创建成功
    //request节点
    qDebug() << "request";
    CreateSecondNode(doc,msgEmt,fileList);
    //保存文件
    file.encodeName("UTF-8");
    file.open(QIODevice::WriteOnly);
    QTextStream stream(&file);
    doc.save(stream,4);
    file.close();
}
void Widget::CreateSecondNode(QDomDocument &doc, QDomElement &root,
                              XMLFileList &fileList)
{
    //二级节点
    QDomElement reqRoot = doc.createElement("Request");
    QDomElement respRoot = doc.createElement("Response");
    root.appendChild(reqRoot);
    root.appendChild(respRoot);
    ui->textEditLog->append("<Request>");
    bool reqLog = true;
    bool transcodeFlag = true;
     for(int i=0; i < fileList.count();i++)
     {
         QDomElement childEmt = doc.createElement(fileList.at(i).m_strId);
         QDomAttr childAttr;
         QString requestPath =fileList.at(i).m_strRequestPath;
         QString responsePath = fileList.at(i).m_strResponsePath;
         QStringList reqList = requestPath.split("/",QString::SkipEmptyParts);
         QStringList respList =responsePath.split("/",QString::SkipEmptyParts);
         if(transcodeFlag|| respList.size() == 0 && reqList.at(1).toUpper() == "REQUEST")
         {
             //判读是否为Transcode
            if(transcodeFlag)
            {
                transcodeFlag = false;
                childAttr = doc.createAttribute("Request");

            }
            else
            {
                childAttr = doc.createAttribute(reqList.at(1));
            }
            childAttr.setNodeValue(requestPath);
            childEmt.setAttributeNode(childAttr);
            childAttr =doc.createAttribute("value");
            childAttr.setNodeValue(fileList.at(i).m_strValue);
            childEmt.setAttributeNode(childAttr);
            childAttr =doc.createAttribute("Remark");
            childAttr.setNodeValue(fileList.at(i).m_strRemark);
            childEmt.setAttributeNode(childAttr);
            reqRoot.appendChild(childEmt);
            ui->textEditLog->append(QString("<%1    Request =\"%2\"   value = \"%3\"  Remark = \"%4\" />")
                                    .arg(fileList.at(i).m_strId)
                                    .arg(requestPath)
                                    .arg(fileList.at(i).m_strValue)
                                    .arg(fileList.at(i).m_strRemark));



         }
         else
         {
             if(reqLog)
             {
                 reqLog = false;
                 ui->textEditLog->append("</Request>");
                 ui->textEditLog->append("<Response>");
             }
             qDebug() <<"respList.at(1) =" << respList.at(1);
             childAttr = doc.createAttribute(respList.at(1));
             childAttr.setNodeValue(responsePath);
             childEmt.setAttributeNode(childAttr);
             childAttr =doc.createAttribute("value");
             childAttr.setNodeValue(fileList.at(i).m_strValue);
             childEmt.setAttributeNode(childAttr);
             childAttr =doc.createAttribute("Remark");
             childAttr.setNodeValue(fileList.at(i).m_strRemark);
             childEmt.setAttributeNode(childAttr);
             respRoot.appendChild(childEmt);
             ui->textEditLog->append(QString("<%1   Response =\"%2\"    value = \"%3\"      Remark = \"%4\" />")
                                     .arg(fileList.at(i).m_strId)
                                     .arg(responsePath)
                                     .arg(fileList.at(i).m_strValue)
                                     .arg(fileList.at(i).m_strRemark));
         }

     }
     ui->textEditLog->append("</Response>");
     ui->textEditLog->append("</Msg>");
}


void Widget::on_pushButton_clicked()
{
    m_msgID.clear();
    m_AppID.clear();
    m_MAC.clear();
    m_msgID = ui->lineEditMsgID->text().trimmed();
    m_AppID = ui->lineEditAppID->text().trimmed();
    QString filePath = ui->lineEditSourcePath->text().trimmed();
    QString newFilePath = ui->lineEditTargetPath->text().trimmed();
    QString msgName = ui->lineEditName->text().trimmed();
    m_MAC =ui->lineEditMac->text().trimmed();
    if(!filePath.isEmpty() && !newFilePath.isEmpty() && !m_msgID.isEmpty())
    {
        ReadMsg(filePath);
        createXML(newFilePath.trimmed());
        qDebug() << "createXML";
        appendXML(newFilePath,m_XMLList,m_msgID,msgName,m_MAC);

    }
    else
    {
        QMessageBox::information(this,"info","Check LineEdit Data");
        return;
    }
}

bool Widget::eventFilter(QObject *obj, QEvent *e)
{
   //事件处理
   if(obj == ui->lineEditSourcePath)
   {
       QMouseEvent *ev = static_cast<QMouseEvent*>(e);
       if(e->type() == QEvent::MouseButtonDblClick && ev->button() == Qt::LeftButton)
       {
           emit dblMouseButtonSignal("Source");
           return false;
       }

   }
   else if(obj == ui->lineEditTargetPath)
   {
       QMouseEvent *ev = static_cast<QMouseEvent*>(e);
       if(e->type() == QEvent::MouseButtonDblClick && ev->button() == Qt::LeftButton)
       {
           emit dblMouseButtonSignal("Target");
       }
   }
   return QObject::eventFilter(obj, e);
}

void Widget::on_buttonFile_clicked()
{
    //判断程序是否打开
    //请求报文

    ui->buttonFile->setEnabled(false);
    QFile reqFile("./request.txt");
    if(reqFile.exists())
    {
        //reqFile.remove();
    }
    reqFile.open(QIODevice::WriteOnly);
    reqFile.close();
    //请求报文中文
    QFile reqzh_cnFile("./requestRemark.txt");
    if(reqzh_cnFile.exists())
    {
        //reqzh_cnFile.remove();
    }
    reqzh_cnFile.open(QIODevice::WriteOnly);
    reqzh_cnFile.close();

    //响应报文
    QFile respFile("./response.txt");
    if(respFile.exists())
    {
        //respFile.remove();
    }
    respFile.open(QIODevice::WriteOnly);
    respFile.close();
    //中文
    QFile respzh_cnFile("./responseRemark.txt");
    if(respzh_cnFile.exists())
    {
        //respzh_cnFile.remove();
    }
    respzh_cnFile.open(QIODevice::WriteOnly);
    respzh_cnFile.close();
    qDebug() << "open notepad";
    QProcess *p = new QProcess(this);
    p->start("tasklist");
    p->waitForFinished();
    QByteArray res =p->readAllStandardOutput();

    if(res.indexOf("notepad++.exe") == -1)
    {

        QStringList list;
        list <<"./responseRemark.txt" << "./response.txt"<< "./requestRemark.txt" <<"./request.txt" ;
        p->start("notepad++.exe",list);
    }
    else
    {
        QMessageBox::warning(this,"提示",QString("notepad++已在其它实例打开，请先关闭在尝试").toStdString().data());
        ui->buttonFile->setEnabled(true);

    }
    ui->buttonFile->setEnabled(true);

}

void Widget::on_buttonTransMsg_clicked()
{
    //读取文件
    QString AppID ="E59901";
    QString targetFile = QString("./msg_%1.xml").arg(AppID);
    QString msgID ="99NISPLC0008";
    QString tranCodeName = "理财签约";

    QFile file("./request.txt");
    m_resNum =0;
    QStringList resList,resRemarkList;
    QString strLine;
    bool isOk = file.open(QIODevice::ReadOnly);
    if(!isOk)
    {
        qDebug() << "open file fail";
        return;
    }
    while (!file.atEnd())
    {
        m_resNum +=1;
        qDebug() << "m_resNum " <<m_resNum;
        strLine = file.readLine().trimmed();
        strLine =strLine.replace(" ","").replace("<","").replace(">","");
        resList <<strLine;
        qDebug() <<strLine;

    }
    file.close();
    m_resRemarkNum =0;
    QFile resRemarkFile("./requestRemark.txt");
    if(!resRemarkFile.open(QIODevice::ReadOnly))
    {
        return;
    }
    while (!resRemarkFile.atEnd()) {
        m_resRemarkNum +=1;
        strLine = QString::fromLocal8Bit(resRemarkFile.readLine().trimmed());
        //strLine =strLine.replace(" ","").replace("<","").replace(">","");
        resRemarkList << strLine;
    }

    m_resTagLenMax =0;
    m_TrandataDefLen = 0;
    if(m_resNum == m_resRemarkNum && m_resNum !=0 && m_resRemarkNum !=0)
    {
        // request == remark
        for(int i=0; i < m_resNum; i++)
        {
            m_Field.m_strRequestPath = resList.at(i);
            QStringList list = resList.at(i).split("/");
            QString str = list.at(list.length()-1);
            QString strTagName = QString("F_%1_%2").arg(AppID).arg(str);
            m_Field.m_strTagName = strTagName;
            QString strValue = QString("$%1_P_%2").arg(AppID).arg(str);
            m_Field.m_strValue = strValue ;
            QString strRemark = resRemarkList.at(i);
            m_Field.m_strRemark = strRemark;
            m_FieldGatherList.push_back(m_Field);
            m_resTagLenMax = CompareThreeNum(strValue.length(),strTagName.length(),m_resTagLenMax);
            QString mapKey = QString("%1_P_%2").arg(AppID).arg(str);
            int keyLen =mapKey.length();
            m_TrandataDefLen = CompareThreeNum(keyLen,m_TrandataDefLen);
            m_FieldMap.insert(mapKey,strRemark);
        }
    }

    //response
    QFile respfile("./response.txt");
    m_respNum =0;

    QStringList respList,respRemarkList;
    if(!respfile.open(QIODevice::ReadOnly))
    {
        qDebug() << "open file fail";
        return;
    }
    while (!respfile.atEnd())
    {
        m_respNum +=1;
        strLine = respfile.readLine().trimmed();
        strLine =strLine.replace(" ","").replace("<","").replace(">","");
        respList <<strLine;
        qDebug() <<strLine;

    }
    file.close();
    m_respRemarkNum =0;
    QFile respRemarkFile("./responseRemark.txt");
    if(!respRemarkFile.open(QIODevice::ReadOnly))
    {
        return;
    }
    while (!respRemarkFile.atEnd()) {
        m_respRemarkNum +=1;
        strLine = QString::fromLocal8Bit(respRemarkFile.readLine().trimmed());
        //strLine =strLine.replace(" ","").replace("<","").replace(">","");
        respRemarkList << strLine;
    }
    m_respTagLenMax = 0;

    if(m_respNum == m_respRemarkNum && m_respNum !=0 && m_respRemarkNum !=0)
    {
        // response == remark
        for(int i=0; i < m_respNum; i++)
        {
            m_Field.m_strResponsePath = respList.at(i);
            QStringList list = respList.at(i).split("/");
            QString str = list.at(list.length()-1);
            QString strTagName = QString("F_%1_%2").arg(AppID).arg(str);
            m_Field.m_strTagName = strTagName ;
            QString strValue = QString("$%1_RP_%2").arg(AppID).arg(str);
            m_Field.m_strValue = strValue;
            QString strRemark = respRemarkList.at(i);
            m_Field.m_strRemark = strRemark;
            m_FieldGatherList.push_back(m_Field);
            m_respTagLenMax = CompareThreeNum(strValue.length(),strTagName.length(),m_respTagLenMax);
            QString mapKey = QString("%1_RP_%2").arg(AppID).arg(str);
            int keyLen =mapKey.length();
            m_TrandataDefLen = CompareThreeNum(keyLen,m_TrandataDefLen);
            m_FieldMap.insert(mapKey,strRemark);
        }
    }
    qDebug() << "xml transform .";
    //写xml
    QChar space =QChar(' ');
    QFile newFile(targetFile);
    newFile.encodeName("UTF-8");
    if(!newFile.open(QIODevice::WriteOnly))
    {
        return;
    }
    //将数据取出格式化成xml
    QTextStream inStream(&newFile);

    QString xmlHead = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
    QString strBodyBegin = "<Body useHEX=\"false\"> \r\n";
    QString strBodyEnd = "</Body>";
    //<Msg>

    QString msgLabel = QString(4,space).append(QString("<Msg id =\"%1\" mac =\"%2\" name =\"%3\">\r\n").arg(msgID).arg("true").arg(QString::fromLocal8Bit(tranCodeName.toStdString().data())));
    msgLabel +=QString(8,space).append("<Request>\r\n");
    m_resTagLenMax = m_resTagLenMax + 4;
    QString strTranCode = QString("F_TranCode");
    QString strRequest = QString("head3");
    QString strValue = msgID;
    msgLabel +=QString(12,space).append(QString("<%1").arg(strTranCode))
            .append(QString(fillBlankSpace(strTranCode.length(),m_resTagLenMax),space))
            .append("Request=\"%1\"").arg(strRequest)
            .append(QString(fillBlankSpace(strRequest.length(),m_resTagLenMax),space))
            .append(QString("value=\"%1\"").arg(strValue))
            .append(QString(fillBlankSpace(strValue.length(),m_resTagLenMax),space))
            .append(QString("Remark =\"%1\"/>\r\n").arg(QString::fromLocal8Bit("交易代码")));
    qDebug() << " resNum " << m_resNum;

    for(int m=0; m < m_resNum; m++)
    {
        QString strtarName = m_FieldGatherList.at(m).m_strTagName;
        QString strRequest = m_FieldGatherList.at(m).m_strRequestPath;
        QString strValue = m_FieldGatherList.at(m).m_strValue;
        int tagSpace = fillBlankSpace(strtarName.length(),m_resTagLenMax);
        int reqSpace = fillBlankSpace(strRequest.length(),m_resTagLenMax);
        int valSpace = fillBlankSpace(strValue.length(),m_resTagLenMax);
        msgLabel += QString(12,space)
                .append(QString("<%1").arg(strtarName))\
                .append(QString(tagSpace,space))\
                .append(QString("Request=\"%1\"").arg(strRequest))
                .append(QString(reqSpace,space))\
                .append(QString("value=\"%1\"").arg(strValue))\
                .append(QString(valSpace,space))
                .append(QString("Remark =\"%4\"/>\r\n").arg(QString::fromLocal8Bit(m_FieldGatherList.at(m).m_strRemark.toStdString().data())));
                /*QString("<%1 Request=\"%2\" value=\"%3\" Remark =\"%4\"/>\r\n")*/
   }
    msgLabel +=QString(8,space).append("</Request>\r\n");
    qDebug() << "Requst end";
    msgLabel +=QString(8,space).append(QString("<Response>\r\n"));
    m_respTagLenMax = m_respTagLenMax + 4;
    int totalNum = m_FieldGatherList.size();
    for(int k= m_resNum; k < totalNum; k++)
    {
        QString strTarName = m_FieldGatherList.at(k).m_strTagName;
        QString strResponse = m_FieldGatherList.at(k).m_strResponsePath;
        QString strValue = m_FieldGatherList.at(k).m_strValue;
        msgLabel += QString(12,space)
                .append(QString("<%1").arg(strTarName))\
                .append(QString(fillBlankSpace(strTarName.length(),m_respTagLenMax),space))\
                .append(QString("Response=\"%1\"").arg(strResponse))\
                .append(QString(fillBlankSpace(strResponse.length(),m_respTagLenMax),space))\
                .append(QString("value=\"%1\"").arg(strValue))\
                .append(QString(QString(fillBlankSpace(strValue.length(),m_respTagLenMax),space)))
                .append(QString("Remark =\"%1\"/>\r\n""").arg(QString::fromLocal8Bit(m_FieldGatherList.at(k).m_strRemark.toStdString().data())));
                /*QString("<%1    Response=\"%2\"    value=\"%3\"    Remark =\"%4\"/>\r\n")*/
    }
    msgLabel +=QString(8,space).append(QString("</Response>\r\n"));
    msgLabel +=QString(4,space).append(QString("</Msg>\r\n"));
    QString xml = xmlHead + strBodyBegin + msgLabel + strBodyEnd;
        qDebug() << xml;
    inStream << xml; //<< strBodyBegin << msgLabel <<strBodyEnd;
    newFile.close();
    qDebug() << m_FieldGatherList.size();
    for(int i=0; i< m_FieldGatherList.size();i++)
    {
        qDebug() << m_FieldGatherList.at(i).m_strRemark;
        qDebug() << m_FieldGatherList.at(i).m_strTagName;
        qDebug() << m_FieldGatherList.at(i).m_strValue;
        qDebug() << m_FieldGatherList.at(i).m_strRequestPath;
        qDebug() << m_FieldGatherList.at(i).m_strResponsePath;
        qDebug() << "-------------------------------------";

    }
}

void Widget::on_buttonTransdef_clicked()
{
    QString AppID ="E59901";
    QString fileName = QString("%1/TranDataDef_%2.xml").arg(m_tempDir).arg(AppID);
    qDebug() << fileName;
    QFile defFile(fileName);
    defFile.encodeName("UTF-8");
    if(!defFile.open(QIODevice::WriteOnly))
    {
        return;
    }
    QTextStream tStream(&defFile);
    qDebug() << " open file";
    QString xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Cols>\r\n";

    for(QMap<QString,QString>::iterator it= m_FieldMap.begin(); it!= m_FieldMap.end(); it++)
    {
        qDebug() <<it.key() << it.value();
        xml += QString(4,QChar(' ')).append(QString("<%1").arg(it.key()).append(QString(fillBlankSpace(it.key().length(),m_TrandataDefLen),QChar(' ')))
                .append(QString(" value =\"\"   "))
                .append(QString("Remark =\"%1\" /> \r\n").arg(QString::fromLocal8Bit(it.value().toStdString().data()))));
    }
    xml += "</Cols>";

    tStream << xml;
    qDebug() << xml;
    defFile.close();


}

int Widget::fillBlankSpace(int arg1, int arg2)
{
    return arg2 -arg1;
}
int Widget::CompareThreeNum(int arg1, int arg2, int m_arg3)
{
    int max =0;
    if(arg1>=arg2)
    {
        if(arg1 >= m_arg3)
        {
            max =arg1;
        }
        else
        {
            max = m_arg3;
        }
    }
    else
    {
        if(arg2 >= m_arg3)
        {
            max = arg2;
        }
        else
        {
            max = m_arg3;
        }
    }
    return max;
}
